<template>
  <div class="container" style="width: 600px; margin-top: 150px">
    <div class="panel panel-default">
      <div class="panel-heading">登录</div>
      <div class="panel-body">
        <form>
          <div class="form-group">
            <label for="exampleInputEmail1">工号</label>
            <input
              type="text"
              class="form-control"
              v-model="data.username"
              placeholder="用户名"
            />
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">密码</label>
            <input
              type="password"
              class="form-control"
              v-model="data.password"
              placeholder="密码"
            />
          </div>
          <div class="form-group">
            <label class="radio-inline">
              <input
                type="radio"
                name="inlineRadioOptions"
                v-model="data.identity"
                value="1"
              />
              职工
            </label>
            <label class="radio-inline">
              <input
                type="radio"
                name="inlineRadioOptions"
                v-model="data.identity"
                value="0"
              />
              管理员
            </label>
          </div>
          <button type="button" class="btn btn-primary" @click="login">
            登录</button
          >&nbsp;&nbsp;
          <button type="reset" class="btn btn-default">重置</button>
        </form>
        <span>{{ msg }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: {
        username: "",
        password: "",
        identity: "1",
      },
      //登录提醒消息
      msg: "",
    };
  },
  methods: {
    //登录
    login() {
      console.log(
        this.data.username +
          " --- " +
          this.data.password +
          "---" +
          this.data.identity
      );
      this.axios({
        method: "post",
        url: "http://localhost:3000/users",
        data: this.data,
      })
        .then((response) => {
          if (response.data.code === 200) {
            if (this.data.identity === "0") {
              this.$store.dispatch("login", response.data.data);
              this.$router.push("/houtai");
            } else if (this.data.identity === "1") {
              this.$store.dispatch("login", response.data.data);
              this.$router.push("/main");
            }
          }
        })
        .catch((error) => {
          this.msg = error;
        });
    },
    //登录结束
  },
};
</script>
<style>
form{
  width: 480px;
  height: 358;
}
</style>